package com.iemes.controller.qc;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iemes.annotation.SystemLog;
import com.iemes.controller.base.ConstantController;
import com.iemes.controller.index.BaseController;

import com.iemes.entity.ChildTableFormMap;
import com.iemes.entity.InventoryFormMap;
import com.iemes.entity.ProjectJcForMmap;
import com.iemes.mapper.MaterialMapper;
import com.iemes.mapper.ProjuctJcMapper;
import com.iemes.util.Common;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

//项目检验
@Controller
@RequestMapping("/pi/")
public class ProjectInspectionController extends BaseController {
    @Inject
    private ProjuctJcMapper projuctJcMapper;
    @Inject
    private MaterialMapper materialMapper;
    @RequestMapping("openUI")
    public String openUI(Model model){
        String [] btnArray = new String[]{"<button type=\"button\" id=\"addAccount\" class=\"btn btn-primary marR10\">新增</button>","<button type=\"button\" id=\"editAccount\" class=\"btn btn-info marR10\">编辑</button>","<button type=\"button\" id=\"delAccount\" class=\"btn btn-danger marR10\">删除</button>","<button type=\"button\" id=\"associated\" class=\"btn btn-danger marR10\">添加关联</button>","<button type=\"button\" id=\"openAssociated\" class=\"btn btn-danger marR10\">查看关联</button>"};
        model.addAttribute("res", findByRes());
        model.addAttribute("btn",btnArray);
        return Common.BACKGROUND_PATH + "/qc/pi/pi_list";
    }
    //获取全部数据
    @ResponseBody
    @RequestMapping("getPiByPage")
    public PageInfo clientByPage(String pageNow,
                                 String pageSize, String column, String sort) throws Exception {

        String myValue = getPara("myValue");
        if(myValue == null || myValue.equals("")){
            if(pageNow == null){
                PageHelper.startPage(1, 10);
            }else {
                PageHelper.startPage(Integer.parseInt(pageNow), 10);
            }
            PageInfo pageInfo = new PageInfo<>(projuctJcMapper.getAll());
            return pageInfo;

        }else {
            String[] split = myValue.split(",");
            if(split.length > 1){
                if(split[0].equals("name")){
                    //根据名称进行模糊查询
                    PageInfo pageInfo = new PageInfo<>(projuctJcMapper.getData(split[1],"name"));
                    return pageInfo;
                }else {
                    //根据编号进行模糊查询
                    PageInfo pageInfo = new PageInfo<>(projuctJcMapper.getData(split[1],"code"));
                    return pageInfo;
                }
            }else {
                PageInfo pageInfo = new PageInfo<>( projuctJcMapper.getAll());
                return pageInfo;
            }
        }
    }

    //打开
    @RequestMapping("openAdd")
    public String openProcessManagementUI(){
        return Common.BACKGROUND_PATH + "/qc/pi/pi_add";
    }

    //打开关联数据
    @RequestMapping("associated")
    public String openAssociatedUI(Model model){

        model.addAttribute("id",getPara("id"));
        return Common.BACKGROUND_PATH + "/qc/pi/pi_associated";
    }

    @RequestMapping("openAssociatedUI")
    public String openAssociated(Model model){
        model.addAttribute("id",getPara("id"));
        return Common.BACKGROUND_PATH + "/qc/pi/pi_Tovie";
    }

    //获取关联数据
    @ResponseBody
    @RequestMapping("getAssociatedByPage")
    public PageInfo getAssociatedByPage(String pageNow,
                                 String pageSize, String column, String sort) throws Exception {
        String myValue = getPara("id");
        if(pageNow == null){
            PageHelper.startPage(1, 10);
        }else {
            PageHelper.startPage(Integer.parseInt(pageNow), 10);
        }
        PageInfo pageInfo = new PageInfo<>(projuctJcMapper.selRelevance(myValue));
        return pageInfo;
    }

    //开始进行关联
    @ResponseBody
    @RequestMapping("relevance")
    public String relevance(){
        String[] ids = getPara("ids").split(",");
        if (ids.length > 1){
            String id = ids[ids.length-1];//取出最后一个值
            //关联数据
            for (int i = 0; i < ids.length -1; i++) {
                //根据id进行关联
                projuctJcMapper.relevance(id,ids[i]);
            }
        }
        return "success";
    }

    @ResponseBody
    @RequestMapping("pi_addEntity")
//	@Transactional(readOnly=false)//需要事务操作必须加入此注解
//  @SystemLog(module="生产物料控制",methods="物料维护-新增物料清单")//凡需要处理业务逻辑的.都需要记录操作日志
    public String addEntity() throws Exception {
        ProjectJcForMmap projectJcForMmap = getFormMap(ProjectJcForMmap.class);
        int lyId = projuctJcMapper.getAll().size()+1;
        projectJcForMmap.put("id", lyId);
        // 获取request
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        projuctJcMapper.addEntity(projectJcForMmap);

//        String[] itemNoArr = request.getParameterValues("item_no_1");
//        String[] itemUseNumberArr = request.getParameterValues("item_use_number_11");
//        if(null != itemNoArr ){
//            for(int i = 0 ;i<itemNoArr.length;i++){
//                ChildTableFormMap itemBomFormMap = new ChildTableFormMap();
//                itemBomFormMap.put("ly_child_code", itemNoArr[i]);
//                itemBomFormMap.put("child_name", itemUseNumberArr[i]);
//                itemBomFormMap.put("ly_product_id", lyId);
//
//                baseMapper.addEntity(itemBomFormMap);
//            }
//        }
        JSONObject json = new JSONObject();
        json.put("result", ConstantController.SUCCESS);
        return json.toString();
    }

    //打开编辑页面

    @RequestMapping("pi_editUI")
    public String editUI(Model model) throws Exception {
        String id = getPara("id");
        if(Common.isNotEmpty(id)){
            ProjectJcForMmap bomFormMap = baseMapper.findbyFrist("id", id, ProjectJcForMmap.class);
            model.addAttribute("bom", bomFormMap);

            ChildTableFormMap itemBomFormMap = new ChildTableFormMap();
//            itemBomFormMap.put("item_bom_no", bomFormMap.getStr("item_bom_no"));
            List<ChildTableFormMap> itemBom = projuctJcMapper.getList(id);
            System.out.println(id);
            model.addAttribute("item_bom", itemBom);
        }
        return Common.BACKGROUND_PATH + "/qc/pi/pi_edit";
    }

    @ResponseBody
    @RequestMapping("pi_editEntity")
    @Transactional(readOnly=false)//需要事务操作必须加入此注解
 //   @SystemLog(module="生产物料控制",methods="物料维护-修改物料清单")//凡需要处理业务逻辑的.都需要记录操作日志
    public String editEntity() throws Exception {
        ProjectJcForMmap projectJcForMmap = getFormMap(ProjectJcForMmap.class);

        // 获取request
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        //删除当前数据
        projuctJcMapper.deleteByAttribute("id", projectJcForMmap.get("id")+"", ProjectJcForMmap.class);

        int lyId = projuctJcMapper.getAll().size()+1;

        projectJcForMmap.put("id", lyId);

        projuctJcMapper.addEntity(projectJcForMmap);

//        String[] itemNoArr = request.getParameterValues("item_no_1");
//        String[] itemUseNumberArr = request.getParameterValues("item_use_number_11");
//        projuctJcMapper.deleteByAttribute("ly_product_id", projectJcForMmap.get("id")+"", ChildTableFormMap.class);
//        if(null != itemNoArr ){
//            for(int i = 0 ;i<itemNoArr.length;i++){
//                ChildTableFormMap itemBomFormMap = new ChildTableFormMap();
//                itemBomFormMap.put("ly_child_code", itemNoArr[i]);
//                itemBomFormMap.put("child_name", itemUseNumberArr[i]);
//                itemBomFormMap.put("ly_product_id", lyId);
//                baseMapper.addEntity(itemBomFormMap);
//            }
//        }
        //先删除数据
        return "success";
    }

    @ResponseBody
    @RequestMapping("delPi")
    @Transactional(readOnly=false)//需要事务操作必须加入此注解
    @SystemLog(module="设备例检管理",methods="例检信息删除")//凡需要处理业务逻辑的.都需要记录操作日志
    public String delPi() throws Exception {
        String[] ids = getParaValues("ids");
        for (String id : ids) {
            baseMapper.deleteByAttribute("id", id, ProjectJcForMmap.class);
        }
        return "success";
    }
    //查询关联物料
    @ResponseBody
    @RequestMapping("getItemChildFindByPage")
    public PageInfo getItemChildFindByPage(String pageNow,String pageSize) throws Exception{
        //获取id值
        String id = getPara("id");
        if(id == null || id.equals("")){
            if(pageNow == null){
                PageHelper.startPage(1, 10);
            }else {
                PageHelper.startPage(Integer.parseInt(pageNow), 10);
            }
            //分页
            List<InventoryFormMap> inventory = materialMapper.getInventory();
            PageInfo pageInfo = new PageInfo<>(inventory);
            return pageInfo;
        }else {

        }
        if(pageNow == null){
            PageHelper.startPage(1, 10);
        }else {
            PageHelper.startPage(Integer.parseInt(pageNow), 10);
        }
        //分页
        List<InventoryFormMap> inventory = materialMapper.getInventory();
        PageInfo pageInfo = new PageInfo<>(inventory);
        return pageInfo;

    }
}
